import type { PageParam, PageResult } from '@vben/request';

import { requestClient } from '#/api/request';

export namespace InfraDatacenterTableApi {
  /** 数据中枢字段信息 */
  export interface DatacenterTableFields {
    id: number; // 主键
    tableId?: number; // 数据表ID
    fieldName?: string; // 字段名称
    fieldDesc: string; // 字段描述
    dataType?: string; // 数据类型
    javaType?: string; // Java 属性类型
    javaField?: string; // Java 属性名
    dictType?: string; // 字典类型
    required?: boolean; // 是否必填
    options: string; // 扩展项
    primaryKey?: boolean; // 是否主键
  }
  /** * 新增数据中枢列表请求 */
  export interface CreateListReqVO {
    dataSourceConfigId?: number;
    tableNames: string[];
  }
  export interface DatabaseTable {
    name: string;
    comment: string;
  }

  /** 数据中枢信息 */
  export interface DatacenterTable {
    id: number; // 主键
    dataSourceConfigId?: number; // 数据来源配置ID
    deptId?: number; // 部门ID
    tableName?: string; // 数据表名
    tableDesc: string; // 数据表描述
    actualTable: string; // 物理表名
    status?: number; // 数据状态
    options: string; // 扩展项
    datacentertablefieldsList?: DatacenterTableFields[];
  }
}

/** 查询数据中枢分页 */
export function getDatacenterTablePage(
  data: PageParam,
  desensitize: boolean,
  encrypt: boolean,
) {
  return requestClient.post<
    PageResult<InfraDatacenterTableApi.DatacenterTable>
  >('/infra/datacenter-table/page', data, { desensitize, encrypt });
}

/** 查询数据中枢详情 */
export function getDatacenterTable(id: number) {
  return requestClient.get<InfraDatacenterTableApi.DatacenterTable>(
    `/infra/datacenter-table/get?id=${id}`,
  );
}
/** 获取数据库中可导入的表信息 */
export function getDatabaseTableList(params: any) {
  return requestClient.get<InfraDatacenterTableApi.DatabaseTable[]>(
    '/infra/datacenter-table/get-database-table',
    { params, timeout: 300_000 },
  );
}
/** 基于数据库的表结构，创建代码生成器的表定义 */
export function createDatebaseList(
  data: InfraDatacenterTableApi.CreateListReqVO,
) {
  return requestClient.post('/infra/datacenter-table/create-list', data);
}
/** 新增数据中枢 */
export function createDatacenterTable(
  data: InfraDatacenterTableApi.DatacenterTable,
) {
  return requestClient.post('/infra/datacenter-table/create', data);
}

/** 修改数据中枢 */
export function updateDatacenterTable(
  data: InfraDatacenterTableApi.DatacenterTable,
) {
  return requestClient.put('/infra/datacenter-table/update', data);
}

/** 删除数据中枢 */
export function deleteDatacenterTable(id: number) {
  return requestClient.delete(`/infra/datacenter-table/delete?id=${id}`);
}

/** 批量删除数据中枢 */
export function deleteDatacenterTableList(ids: number[]) {
  return requestClient.delete(
    `/infra/datacenter-table/delete-list?ids=${ids.join(',')}`,
  );
}

/** 导出数据中枢 */
export function exportDatacenterTable(params: any) {
  return requestClient.download('/infra/datacenter-table/export-excel', {
    params,
  });
}

// ==================== 子表（数据中枢字段） ====================

/** 获得数据中枢字段列表 */
export function getDatacenterTableFieldsListByTableId(tableId: number) {
  return requestClient.get<InfraDatacenterTableApi.DatacenterTableFields[]>(
    `/infra/datacenter-table/datacenter-table-fields/list-by-table-id?tableId=${tableId}`,
  );
}
